package lt.marsrutai.mobile.android.adapter;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopMapIntentHolder;
import lt.marsrutai.mobile.android.model.Track;
import lt.marsrutai.mobile.android.model.TrackBookmark;
import lt.marsrutai.mobile.android.model.enums.TrackType;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


public class TrackAdapter extends ListViewAdapter<Track> {

    String scopeFolder;

    int separatorPos = -1;

    public TrackAdapter(Activity activity, String scopeFolder, List<Track> tracks) {
        super(activity, R.layout.tracks_item);

        this.scopeFolder = scopeFolder;

        setTracks(tracks);
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        if (position == separatorPos) {
            TextView separatorView = (TextView) inflater.inflate(R.layout.list_item_separator, null);
            separatorView.setText(R.string.changed_tracks);
            return separatorView;
        } else {
            position = resolveItemPosition(position);
        }


        final Track o = getItem(position);

        if (o != null) {
            TextView trackName = (TextView) view.findViewById(R.id.track_name);
            if (trackName != null) {
                trackName.setText(o.getName());
            }
            ImageButton mapButton = (ImageButton) view.findViewById(R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    StopMapIntentHolder intentHolder = new StopMapIntentHolder(activity);
                    intentHolder.setScopeFolder(scopeFolder);
                    intentHolder.setTrackId(o.getId());
                    activity.startActivity(intentHolder.getIntent());
                }
            });

            final ImageButton starButtonOff = (ImageButton) view.findViewById(R.id.star_off);
            final ImageButton starButtonOn = (ImageButton) view.findViewById(R.id.star_on);

            final TrackBookmark trackBookmark = new TrackBookmark();
            trackBookmark.setScope(scopeFolder);
            trackBookmark.setTrackId(o.getId());

            starButtonOff.setFocusable(false);
            starButtonOff.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    starButtonOff.setVisibility(View.GONE);
                    starButtonOn.setVisibility(View.VISIBLE);

                    AppContext.getBookmarkService().addTrackBookmark(trackBookmark);
                }
            });

            starButtonOn.setFocusable(false);
            starButtonOn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    starButtonOn.setVisibility(View.GONE);
                    starButtonOff.setVisibility(View.VISIBLE);

                    AppContext.getBookmarkService().removeTrackBookmark(trackBookmark);
                }
            });

            if (AppContext.getBookmarkService().containsTrackBookmark(trackBookmark)) {
                starButtonOff.setVisibility(View.GONE);
                starButtonOn.setVisibility(View.VISIBLE);
            }
            else {
                starButtonOn.setVisibility(View.GONE);
                starButtonOff.setVisibility(View.VISIBLE);
            }

        }
        return view;
    }

    public void setTracks(List<Track> tracks)
    {
        if (null == tracks) {
            return;
        }

        for (int i = 0; i < tracks.size(); i++) {
            add(tracks.get(i));

            if (separatorPos == -1 && TrackType.CHANGED.equals(tracks.get(i).getTrackType())) {
                separatorPos = i;
            }
        }
    }

    @Override
    public int getCount() {
        return super.getCount() + (isSeparatorNeeded() ? 1 : 0);
    }

    private boolean isSeparatorNeeded() {
        return separatorPos > 0;
    }

    public int resolveItemPosition(int position) {
        if (isSeparatorNeeded()) {
            if (position > separatorPos) {
                return --position;
            } else if (position == separatorPos) {
                return -1;
            }
            else {
                return position;
            }
        }
        else {
            return position;
        }
    }
}
